import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {Observable} from 'rxjs/Observable';
import {FaultLevel} from '../entity/fault-level.model';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class FaultLevelService {
  private readonly param = 'faultLevels';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }

  /**
   * 获取报警等级
   *
   * @returns {Promise<FaultLevel[]>}
   * @memberof FaultLevelService
   */
  getFaultLevels(): Promise<FaultLevel[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data as FaultLevel[])
      .catch(this.handleError);
  }

  /**
   * 获取报警等级和声音
   *
   * @returns
   * @memberof FaultLevelService
   */
  getFaultLevelsWithSound(): Promise<FaultLevel[]> {
    return this.http.get(`${this.url}/sound`)
      .toPromise()
      .then(response => response.data as FaultLevel[])
      .catch(this.handleError);
    /* let levels: FaultLevel[] = [
        new FaultLevel(1, "高", 1, true, "#FC1829", "", 99, "../../../../assets/media/alarmlevel3.wav",0),
        new FaultLevel(2, "中", 1, true, "#B78E00", "", 2, "../../../../assets/media/alarmlevel2.wav",0),
        new FaultLevel(3, "低", 1, true, "#006400", "", 1, "../../../../assets/media/alarmlevel1.wav",0),
    ];
    return Promise.resolve(levels); */
  }

  /**
   * 根据分类code获取报警等级和声音列表
   *
   * @returns
   * @memberof FaultLevelService
   */
  getBasicFaultLevelListByDeviceCategoryCodes(codes: string[]): Promise<FaultLevel[]> {
    return this.http.get(`${this.url}/deviceCategoryCodes/${codes}`)
      .toPromise()
      .then(response => response.data as FaultLevel[])
      .catch(this.handleError);
  }

  create(faultLevel: FaultLevel): Promise<FaultLevel> {
    return this.http.post(this.url, JSON.stringify(faultLevel))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '200':
              return result.data as FaultLevel;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  update(faultLevel: FaultLevel): Promise<FaultLevel> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(faultLevel))
      .toPromise()
      .then(() => faultLevel)
      .catch(this.handleError);
  }

  //根据Name判断是否已经存在
  hasStateName(faultLevelName: string, faultLevelId: number): Observable<boolean> {
    const url = `${this.url}/faultLevelName/${faultLevelName}/faultLevelId/${faultLevelId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }).catch(this.handleError);
  }

  hasStateColor(faultLevelColor: string, faultLevelId: number): Observable<boolean> {
    faultLevelColor = faultLevelColor.replace(/\#/, '%23');
    const url = `${this.url}/faultLevelColor/${faultLevelColor}/faultLevelId/${faultLevelId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }

    }).catch(this.handleError);
  }

  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }

  batchDelete(valueIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${valueIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }


}


// WEBPACK FOOTER //
// ./src/app/service/fault_level.service.ts
